package net.oschina.app.fragment.newsview;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.R;
import net.oschina.app.adapter.newsadapter.NewsAdapter;
import net.oschina.app.bean.News;
import net.oschina.app.bean.NewsList;
import net.oschina.app.constant.Constants;
import net.oschina.app.fragment.BaseFragment;
import net.oschina.app.listener.OnRecycleViewItemClickListener;
import net.oschina.app.ui.NewsDetailActiviy;
import net.oschina.app.util.URLGet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuer on 2017/4/5/005.
 * 综合子页面的基类
 */

public abstract class BaseNewsVPFragment extends BaseFragment {
    private List<News> mLists = new ArrayList<News>();
    private XRecyclerView mRv;
    protected NewsAdapter mNewsAdapter;

    //初始化视图
    @Override
    protected View initView() {
        mRv = (XRecyclerView) View.inflate(getContext(), R.layout.news_rv, null);
        mRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        return mRv;
    }

    //到网络获取数据
    @Override
    protected void setData() {
        URLGet urlGet = new URLGet();
        int index = 0;
        String news_path = Constants.News_Path + getIndex() + ".xml";
        urlGet.getData(news_path, NewsList.class, new URLGet.OnURLGetLitener<NewsList>() {
            @Override
            public void showSuccess(NewsList bean) {
                NewsList mnewsList = bean;
                mLists = mnewsList.getList();
                dealCommonSuccess(mLists);
            }

            @Override
            public void showError(Exception e) {
                dealCommonFail();
            }
        });
    }

    //成功界面
    @Override
    protected void showSuccess() {
        //添加头部
        // initRecyclerHeadView(mRv);
        mNewsAdapter = new NewsAdapter(mLists);
        mRv.setAdapter(mNewsAdapter);
        // 静止下拉刷新，加载更多
        mRv.setLoadingMoreEnabled(true);
        mRv.setPullRefreshEnabled(false);
        loadMore(mRv);// 加载更多
        initEvent(); // 处理事件
        changeView(mRv);// 更新处理
    }

    //初始化事件
    private void initEvent() {
        mNewsAdapter.setOnItemClickListener(new OnRecycleViewItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                position = position - 1;
                Log.d("position:", "-----" + position);
                Intent intent = new Intent(getActivity(), NewsDetailActiviy.class);
                News news = mLists.get(position);
                int id = news.getId();
                intent.putExtra("id", id);
                startActivity(intent);
            }
        });
    }

    // 添加头部
    private void initRecyclerHeadView(XRecyclerView mRv) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View headView = inflater.inflate(R.layout.news_rv_header_view, mRv, false);
        mRv.addHeaderView(headView);
    }

    //加载更多
    private void loadMore(XRecyclerView mRv) {
        mRv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
            }

            @Override
            public void onLoadMore() {
                URLGet urlGet = new URLGet();
                int index = 0;
                urlGet.getData(Constants.News_Path + index + ".xml", NewsList.class, new URLGet.OnURLGetLitener<NewsList>() {
                    @Override
                    public void showSuccess(NewsList bean) {
                        NewsList mnewsList = bean;
                        List<News> mLists1 = mnewsList.getList();
                        mLists.addAll(mLists1);
                        mNewsAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void showError(Exception e) {
                        dealCommonFail();
                    }
                });
            }
        });
    }

    protected abstract String getIndex();
}
